/*
 * Copyright (C) 2008 Mo Chen <withinsea@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
(function ($) {
	
	$.ex = function (select, fn, staticFn) {
		
		var base = this;
		var standin = function () {};
			standin.prototype = base.prototype;
		var exinit = function () { $.fn.init.apply(this, arguments); };
			exinit.prototype = new standin();
			
		staticFn = ((select && (typeof select != "object")) ? arguments[2] : arguments[1]) || {};
		fn = ((select && (typeof select != "object")) ? arguments[1] : arguments[0]) || {};
		
		var fselect = (!select || typeof select == "object") ? function (selector, context) {
			return base(selector, context);
		} : (typeof select == "string") ? function (selector, context) {
			return $(select, base(selector, context));
		} : select;
				
		var ex = function (selector, context) {
			return new exinit(fselect(selector, context));
		};
		
		for (var name in base) {
			if (name != "prototype" && base[name] != undefined) {
				ex[name] = base[name];
			}
		}
		
		$.extend(ex, {
			prototype: exinit.prototype,
			fn: exinit.prototype
		}, staticFn);
		
		$.extend(ex.fn, fn);
		
		return ex;
	};
	
}) (jQuery);
